package com.sskj.one.mine;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.UserInfo;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.one.R;
import com.sskj.one.component.DaggerUserDataComponent;

import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/04
 */
public class UserInfoActivity extends BaseActivity<UserInfoPresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_head)
    TextView tvHead;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_set_nick_name)
    TextView tvSetNickName;
    @Inject
    UserViewModel userViewModel;

    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_user_info;
    }

    @Override
    public UserInfoPresenter getPresenter() {
        return new UserInfoPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        userViewModel.update();
    }

    @Override
    public void initView() {
        tvTitle.setText("个人资料");
        DaggerUserDataComponent.create().inject(this);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("1", R.mipmap.head_one);
        map.put("2", R.mipmap.head_two);
        map.put("3", R.mipmap.head_three);
        map.put("4", R.mipmap.head_four);
        map.put("5", R.mipmap.head_five);
        map.put("6", R.mipmap.head_six);
        map.put("7", R.mipmap.head_seven);
        map.put("8", R.mipmap.head_eight);
        map.put("9", R.mipmap.head_nine);
        map.put("10", R.mipmap.head_ten);
        map.put("11", R.mipmap.head_eleven);
        map.put("12", R.mipmap.head_twelve);
        map.put("13", R.mipmap.head_thirteen);
        map.put("14", R.mipmap.head_fourteen);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                tvSetNickName.setText(userData.getNickname());
                if (isEmpty(userData.getUpic())) {
                    ivHead.setVisibility(View.INVISIBLE);
                } else {
                    ivHead.setVisibility(View.VISIBLE);
                    ImageUtil.setImage(map.get(userData.getUpic()), ivHead);
                }
            } else {
                userData = null;
            }

        });

    }

    @Override
    public void initData() {
        ClickUtil.click(300, tvHead, () -> {
            HeadActivity.start(UserInfoActivity.this);
        });
        ClickUtil.click(300, tvNickName, () -> {
            NickNameActivity.start(UserInfoActivity.this);
        });

    }

    public static void start(Activity context) {
        Intent intent = new Intent(context, UserInfoActivity.class);
        context.startActivity(intent);
    }
}
